/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#include "rgcpp/tools/MD5Generator.h"

namespace rgcpp
{

MD5Generator::MD5Generator()
{
	state=false;
	MD5_Init(&md5_ctx);
}

MD5Generator::~MD5Generator()
{

}

void MD5Generator::init()
{
	state=true;
	MD5_Init(&md5_ctx);
}

void MD5Generator::update(const char* data, const unsigned length)
{
	if(!state)
	{
		Throw(IllegalStateException("MD5Generator not initialized!!!"));
	}

	MD5_Update(&md5_ctx,data,length);
}

void MD5Generator::digest(char* digest)
{
	if(!state)
	{
		Throw(IllegalStateException("MD5Generator not initialized!!!"));
	}

	MD5_Final((unsigned char*)digest,&md5_ctx);
}

void MD5Generator::digest(const char* data, const unsigned int length, char* digest)
{
	MD5_CTX md5_ctx;

	MD5_Init(&md5_ctx);
	MD5_Update(&md5_ctx,data,length);
	MD5_Final((unsigned char*)digest,&md5_ctx);
}

}
